/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.s2aei.view;

import com.s2aei.controller.AvaliadorController;
import com.s2aei.model.Usuario;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Fabio
 */
public class JanelaListarAvaliadores extends javax.swing.JFrame {

    /**
     * Creates new form JanelaListarAvaliadores
     */
    public JanelaListarAvaliadores() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaAva = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        tabelaAva.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tabelaAva);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("AVALIADORES");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(15, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(160, 160, 160))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 15, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        setSize(new java.awt.Dimension(416, 338));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JanelaListarAvaliadores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JanelaListarAvaliadores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JanelaListarAvaliadores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JanelaListarAvaliadores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JanelaListarAvaliadores().setVisible(true);
            }
        });
    }

        public void preencheTabela(){
        AvaliadorController avController = new AvaliadorController();
        //JTable tabelaAdmins = this.getTabela();
        DefaultTableModel tableModel = new DefaultTableModel(){
                    @Override
                    public boolean isCellEditable(int row, int column){
                        return false;
                    }
                };
        tableModel.addColumn("Id");
        tableModel.addColumn("Nome");
        tableModel.addColumn("Email");
        tableModel.addColumn("Data de Cadastro");
        tableModel.addColumn("Ultima Modificação");
        tableModel.addColumn("Status");
        tableModel.addColumn("Area");
        tableModel.addColumn("Formação");
        tabelaAva.setModel(tableModel);
        List<Usuario> avaList = avController.listarAvaliador();
        SimpleDateFormat formataDataCadastro = new SimpleDateFormat("dd/MM/yyy");
        SimpleDateFormat formataDataModificado = new SimpleDateFormat("dd/MM/yyy HH:mm:ss");
        for(Usuario u : avaList){
            String dataCad = formataDataCadastro.format(u.getDataCadastro());
            Date dataModificado = new Date();
            String dataMod = formataDataModificado.format(dataModificado);
            if(u.getDataModificado() != null){
                dataMod = formataDataModificado.format(u.getDataModificado());
            }            
            tableModel.addRow(new Object[]{u.getUserId(), u.getNome(), u.getEmail(), dataCad, dataMod, u.getStatus().getTipo(), u.getAvaliador().getArea(), u.getAvaliador().getFormacao()});
        }
        addDoubleClick();
        setVisible(true);
    }
    
    public JTable getTabela(){
        return tabelaAva;
    }
    
    public void addDoubleClick(){
        tabelaAva.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseClicked(MouseEvent e){
                if(e.getClickCount()==2){
                    JanelaCadastroAvaliador janela = new JanelaCadastroAvaliador();
                    JTable target = (JTable)e.getSource();
                    int row = target.getSelectedRow();
                    janela.getIdField().setText(target.getValueAt(row, 0).toString());
                    janela.getIdField().setEditable(false);
                    janela.getNomeField().setText(target.getValueAt(row, 1).toString());
                    janela.getEmailField().setText(target.getValueAt(row, 2).toString());
                    janela.getAreaField().setText(target.getValueAt(row, 6).toString());
                    janela.getFormacaoField().setText(target.getValueAt(row, 7).toString());
                    janela.getConfirmaBtn().setVisible(false);
                    janela.getEditarBtn().setVisible(true);
                    janela.setVisible(true);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabelaAva;
    // End of variables declaration//GEN-END:variables
}
